package com.coolt.client.components;

import java.util.List;

import com.coolt.client.Messages;
import com.coolt.client.Util;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public abstract class DynamicTable extends VerticalPanel {

	private String title;

	private List listTable;

	private Integer pageNow;

	private Integer pageSize;

	private Integer totalPages;

	private VerticalPanel panelTable = new VerticalPanel();

	private Hyperlink hlFirst;
	private Hyperlink hlPrevious;
	private Hyperlink hlPageBefore;
	private Hyperlink hlPageAfter;
	private Hyperlink hlNext;
	private Hyperlink hlLast;

	protected abstract VerticalPanel showElement(int i);

	protected abstract void carregarLista();

	private Messages msgConstants = GWT.create(Messages.class);

	private Label lbCharging;

	public DynamicTable() {

		setStyleName("frames");
		setSize("600", "500");
		setHorizontalAlignment(ALIGN_CENTER);
		setVerticalAlignment(VerticalPanel.ALIGN_TOP);
	}

	public void inicia(int pgSize) {
		setPageNow(1);
		setPageSize(15);
		add(Util.tituloFrames(title));
		panelTable.setWidth("600");
		panelTable.setHorizontalAlignment(HorizontalPanel.ALIGN_CENTER);
		add(panelTable);
	}

	public void writeCharging() {
		panelTable.clear();
		lbCharging = new Label(msgConstants.messageCharging());
		lbCharging.setStyleName("messages");
		panelTable.add(lbCharging);
	}

	public HorizontalPanel navigationBar() {

		HorizontalPanel hp = new HorizontalPanel();
		if (totalPages > 1) {

			hp.setStyleName("navigationBar");
			hp.setSpacing(13);

			hlFirst = new Hyperlink("<<", "first");
			hlPrevious = new Hyperlink("<", "previous");
			hlPageBefore = new Hyperlink(Integer.toString(pageNow - 1),
					"pageBefore");
			hlPageAfter = new Hyperlink(Integer.toString(pageNow + 1),
					"pageAfter");
			hlNext = new Hyperlink(">", "next");
			hlLast = new Hyperlink(">>", "last");
			Label lbPageNow = new Label(pageNow.toString());

			hp.add(hlFirst);
			hp.add(hlPrevious);
			hp.add(hlPageBefore);
			hp.add(lbPageNow);
			hp.add(hlPageAfter);
			hp.add(hlNext);
			hp.add(hlLast);

			if (pageNow < 2) {
				hlPageBefore.setVisible(false);
				hlFirst.setVisible(false);
				hlPrevious.setVisible(false);

			} else if (pageNow.equals(totalPages)) {
				hlPageAfter.setVisible(false);
				hlNext.setVisible(false);
				hlLast.setVisible(false);
			}

		}
		return hp;
	}

	public VerticalPanel listar() {

		VerticalPanel vp = new VerticalPanel();
		HorizontalPanel hp = new HorizontalPanel();

		for (int i = 0; (i < listTable.size() && i < 15); i++) {
			if ((i % 5) == 0) {
				hp = new HorizontalPanel();
				hp.setHorizontalAlignment(ALIGN_CENTER);
				hp.setWidth("100%");
				hp.setSpacing(5);
				vp.add(hp);
			}
			hp.add(showElement(i));
		}
		return vp;
	}

	public void displayTable() {

		panelTable.clear();
		panelTable.setHorizontalAlignment(HorizontalPanel.ALIGN_RIGHT);
		panelTable.add(navigationBar());
		panelTable.setHorizontalAlignment(HorizontalPanel.ALIGN_CENTER);
		panelTable.add(listar());
	}

	public void carregarLinkPaginas() {

		if (totalPages > 1) {
			ClickListener clFirst = new ClickListener() {
				public void onClick(Widget sender) {
					setPageNow(1);
					carregarLista();
				}
			};
			getHlFirst().addClickListener(clFirst);

			ClickListener clPrevious = new ClickListener() {
				public void onClick(Widget sender) {
					setPageNow(getPageNow() - 1);
					carregarLista();
				}
			};
			getHlPrevious().addClickListener(clPrevious);
			getHlPageBefore().addClickListener(clPrevious);

			ClickListener clAfter = new ClickListener() {
				public void onClick(Widget sender) {
					setPageNow(getPageNow() + 1);
					carregarLista();
				}
			};
			getHlNext().addClickListener(clAfter);
			getHlPageAfter().addClickListener(clAfter);

			ClickListener clLast = new ClickListener() {
				public void onClick(Widget sender) {
					setPageNow(getTotalPages());
					carregarLista();
				}
			};
			getHlLast().addClickListener(clLast);
		}
	}

	public void displayFailure() {
		panelTable.clear();
		panelTable.add(new Label("Communication failed"));
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public List getListTable() {
		return listTable;
	}

	public void setListTable(List listTable) {
		this.listTable = listTable;
	}

	public Integer getPageNow() {
		return pageNow;
	}

	public void setPageNow(Integer pageNow) {
		this.pageNow = pageNow;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public Integer getTotalPages() {
		return totalPages;
	}

	public void setTotalPages(Integer totalPages) {
		this.totalPages = totalPages;
	}

	public Hyperlink getHlFirst() {
		return hlFirst;
	}

	public void setHlFirst(Hyperlink hlFirst) {
		this.hlFirst = hlFirst;
	}

	public Hyperlink getHlPrevious() {
		return hlPrevious;
	}

	public void setHlPrevious(Hyperlink hlPrevious) {
		this.hlPrevious = hlPrevious;
	}

	public Hyperlink getHlPageBefore() {
		return hlPageBefore;
	}

	public void setHlPageBefore(Hyperlink hlPageBefore) {
		this.hlPageBefore = hlPageBefore;
	}

	public Hyperlink getHlPageAfter() {
		return hlPageAfter;
	}

	public void setHlPageAfter(Hyperlink hlPageAfter) {
		this.hlPageAfter = hlPageAfter;
	}

	public Hyperlink getHlNext() {
		return hlNext;
	}

	public void setHlNext(Hyperlink hlNext) {
		this.hlNext = hlNext;
	}

	public Hyperlink getHlLast() {
		return hlLast;
	}

	public void setHlLast(Hyperlink hlLast) {
		this.hlLast = hlLast;
	}

	public VerticalPanel getPanelTable() {
		return panelTable;
	}

	public void setPanelTable(VerticalPanel panelTable) {
		this.panelTable = panelTable;
	}

}
